#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2015年2月5日

@author: chenyongbing
'''
import sys,os,datetime
#current_dir = os.path.dirname(__file__)
current_dir = os.path.split(os.path.realpath(__file__))[0]
import logging
import commands

from logging.handlers import RotatingFileHandler

level = logging.INFO

Rthandler = RotatingFileHandler('/var/log/pybaklog.log', maxBytes=10*1024*1024,backupCount=0)
Rthandler.setLevel(level)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
Rthandler.setFormatter(formatter)
logging.getLogger('').addHandler(Rthandler)

console = logging.StreamHandler()
console.setLevel(level)
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)

class RawdataBackup(object):
    '''
    classdocs
    '''


    def __init__(self,delRaw=False,testing=False,project = 'rawdata'):
        '''
        Constructor
        '''
        self.delRaw = delRaw
        self.testing = testing
        self.project = project
#         self.rawhost = myLocalConfig.rawdata_host['host']
#         self.rawpasswd = myLocalConfig.rawdata_host['passwd']
#         self.rawuser = myLocalConfig.rawdata_host['user']
    def get_slist_between_st_et(self,st=None,et=None):
        u'''获取2个时间间的所有日期'''
        dlist = []
        count = 0
        if st==None and et==None:
            st = et = datetime.date.today().strftime('%Y-%m-%d')
        elif st==None and et!=None:
            st = et
        elif st!=None and et==None:
            et = st
        nday = (datetime.date(int(st[:4]),int(st[5:7]),int(st[8:]))+ datetime.timedelta(-1)).strftime('%Y-%m-%d')
        while nday < et:
            nday = (datetime.date(int(st[:4]),int(st[5:7]),int(st[8:]))+ datetime.timedelta(count)).strftime('%Y-%m-%d')
            count+=1
            dlist.append(nday)
        return dlist 
        
    def dist_file_name(self,filename):
        filepath = os.path.dirname(filename)
        i=0
        if os.path.exists(filename):
            while 1:
                i+=1
                bfile = filename+'.bak.%s'%i
                if os.path.exists(bfile):
                    continue
                else:
                    break
            
            while 1:
                if not  os.path.exists(filename):break
                ret = self.run_command('mv %s %s'%(filename,bfile))
                if ret == True:
                    logging.info('move File %s To %s Done.'%(filename,bfile))
                    return
                else:
                    logging.error('move File %s To %s Faild.'%(filename,bfile))
    def run_command(self,cmd):
        rets = commands.getstatusoutput(cmd)
        if rets[0]==0:
             return True
        errMsg =rets[1]
        logging.error('Run cmd %s Faild.'%cmd)
        logging.error(errMsg)
        return False
        
    def rawdata_backup(self,day=None,rawpath='/usr/java/data/rawdata/netty/',backuppath='/Udisk/rawdata/'):
        if day==None:
            day = (datetime.date.today()-datetime.timedelta(1)).strftime('%Y-%m-%d')
        rawfile = '%s.%s.txt'%(self.project,day)
        rawdate = day
        if not os.path.exists(rawpath+'/'+rawfile) and not os.path.exists(rawpath+'/'+rawdate):
            logging.error('%s File %s  Not Exists.'%(self.project,rawfile))
            if not self.testing:sys.exit()
        
        
        rawtarfile = rawpath+'/'+rawfile+'.tar.gz'
        if not self.testing:os.chdir(rawpath)
        if os.path.exists(rawpath+'/'+rawfile):
            
            cmd = 'tar -czvf %s %s'%(rawtarfile,rawfile)
        else:
            if not self.testing:os.chdir(rawpath+'/'+rawdate)
            #rawtarfile = rawpath+'/rawdata.%s'%rawdate+'.tar.gz'
            cmd = 'tar -czvf %s %s'%(rawtarfile,'%s.%s.*.txt'%(self.project,day))
        logging.info(cmd)
        
        while not self.testing:
            if os.path.exists(rawtarfile):
                checkCmd = 'gzip -t %s'%rawtarfile
                checkRet = self.run_command(checkCmd)
                if checkRet==True:
                    logging.info('Tar File %s Testing Done.'%rawtarfile)
                    break
                else:
                    logging.error('Tar File %s Testing Faild.'%rawtarfile)
            
            ret = self.run_command(cmd)
            
            if ret==True:
                logging.info('Tar %s File %s Done.'%(self.project,rawfile))
            else:
                logging.error('Tar %s File %s Faild.'%(self.project,rawfile))
                
                
        if backuppath!=None:
            backupfile = backuppath+'/'+rawfile+'.tar.gz'
            self.dist_file_name(backupfile)
            copyCmd = 'cp %s %s'%(rawtarfile,backupfile)
            logging.info(copyCmd)
            while not self.testing:
                if os.path.exists(backupfile):
                    logging.warning('%s BackupFile %s Already Exists.'%(self.project,backupfile))
                    break
                ret = self.run_command(copyCmd)
                if ret==True:
                    logging.info('Copy Tar %s File %s Done.'%(self.project,rawfile))
                    break
                else:
                    logging.error('Copy Tar %s File %s Faild.'%(self.project,rawfile))
        if self.delRaw!=None:
            delRawDay = (datetime.date.today() - datetime.timedelta(self.delRaw)).strftime('%Y-%m-%d')
            delRawFile = rawpath+'/'+delRawDay
            delRaw_bakFile = backuppath+'/%s.%s.txt.tar.gz'%(self.project,delRawDay)
            
            rmCmd = 'rm -rf %s | grep -v "tar.gz"'%(delRawFile)
            logging.info(rmCmd)
            if not os.path.exists(delRaw_bakFile):
                logging.error('BackUp File %s not exists ; You Can Not Del It.'%delRaw_bakFile)
            if self.testing==False:
                
                commands.getoutput(rmCmd)
                
            
    
if __name__ == '__main__':
    import argparse,re,datetime
    parser = argparse.ArgumentParser(description='args')
    parser.add_argument('--day',metavar=u"",default=None)
    parser.add_argument('--rawpath',metavar=u"rawdata path",default='/usr/java/data/rawdata/netty/')
    parser.add_argument('--backuppath',metavar=u"backup path",default='/Udisk/rawdata/')
    parser.add_argument('--startTime',metavar=u"",default=None)
    parser.add_argument('--endTime',metavar=u"",default=None)
    parser.add_argument('--delRaw',metavar=u"del N day ago",default=None,type=int)
    parser.add_argument('--project',metavar=u"",default='rawdata')
    parser.add_argument('--testing',action='store_true',help='testing')
    #parser.add_argument('--delRawAgo',metavar=u"删除 3天前的rawdata;当 delRaw = True 时生效",default=3,type = int)
    args = parser.parse_args()
    day = args.day
    rawpath = args.rawpath
    backuppath = args.backuppath
    startTime = args.startTime
    endTime = args.endTime
    delRaw = args.delRaw
    testing = args.testing
    project = args.project
    #delRawAgo = args.delRawAgo
    myRawdataBackup = RawdataBackup(delRaw=delRaw,testing=testing,project = project)
#     day = '2014-08-26'
#     rawpath = '/Users/chenyongbing/Documents/workspace/AutoTesting/rawdata/2014-08-26'
#     backuppath = '/Users/chenyongbing/Documents/workspace/AutoTesting/rawdata/'
    if day==None:
        day = (datetime.date.today()-datetime.timedelta(1)).strftime('%Y-%m-%d')
    days = [day]
    if startTime!=None and endTime!=None:
        days = myRawdataBackup.get_slist_between_st_et(startTime, endTime)
    for day in days:
        logging.info('start backup date %s'%day)
        myRawdataBackup.rawdata_backup(day=day,rawpath=rawpath,backuppath=backuppath)